I looked at the cars parked on campus and what states they are from and their colors.
# use min/max lat/long to set map imagery boundaries
buffer <- 0.001
min_lat <- min(cars$lat) - buffer
max_lat <- max(cars$lat) + buffer
min_lon <- min(cars$lon) - buffer
max_lon <- max(cars$lon) + buffer
CampusMap <- openmap(
upperLeft = c(max_lat, min_lon),
lowerRight = c(min_lat, max_lon),
type = "esri-imagery", zoom = 18, mergeTiles = TRUE)
APSU <- openproj(CampusMap, projection = "+proj=longlat +ellps=WGS84 +units=m +no_defs")
autoplot.OpenStreetMap(APSU) +
geom_point(data = cars, aes(x = lon, y = lat), color = cars$color, size = 3, alpha = 0.9) +
labs(x = "Longitude", y = "Latitude") +
annotation_scale(location = "br", width_hint = 0.3) + # scale bar
annotation_north_arrow(location = "tr", which_north = "true",
style = north_arrow_orienteering(fill = c("black","black"), text_size = 0,))
## Using plotunit = 'm'
## Warning in draw_panel(...): True north is not meaningful without coord_sf()
# Convert your cars df to sf points
cars_sf <- st_as_sf(cars, coords = c("lon", "lat"), crs = 4326)
# Get bounding box of your cars (or the same as your map area)
bbox <- st_bbox(cars_sf)
# Convert bbox to sf polygon for coord_sf alignment
bbox_sf <- st_as_sfc(bbox)
autoplot.OpenStreetMap(APSU) +
geom_sf(data = cars_sf, aes(color = color), size = 3, alpha = 0.8, inherit.aes = FALSE) +
scale_color_identity() +
coord_sf(xlim = c(bbox["xmin"], bbox["xmax"]),
ylim = c(bbox["ymin"], bbox["ymax"]),
expand = FALSE, crs = 4326) +
annotation_scale(location = "br", width_hint = 0.2) +
annotation_north_arrow(location = "tr", which_north = "true",
style = north_arrow_orienteering(fill = c("black","black"), text_size = 0,)) +
labs(x = "Longitude", y = "Latitude")
## Coordinate system already present.
## ℹ Adding new coordinate system, which will replace the existing one.
library(RColorBrewer)
pal <- colorFactor(
palette = colorRampPalette(brewer.pal(12, "Set3"))(19),
domain = cars$state
)
leaflet(cars) %>%
addTiles() %>%
addProviderTiles("OpenStreetMap", group = "Streets") %>%
addProviderTiles("Esri.WorldImagery", group = "Imagery") %>%
addLayersControl(baseGroups = c("Streets", "Imagery"),
options = layersControlOptions(collapsed = FALSE)) %>%
addScaleBar() %>%
addCircleMarkers(
lng = ~lon,
lat = ~lat,
color = ~pal(state), # colors by state
fillColor = ~pal(state),
fillOpacity = 0.9,
radius = 6,
stroke = FALSE,
label = ~state) %>%
addLegend(
"bottomright",
pal = pal,
values = ~state,
title = "State",
opacity = 1)